﻿using CsvHelper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm1
{
    internal class OutAndInFileInfos
    {
        public static int LoadFileByPath(ref List<PathInfos> fileList, String filePath, bool IsChecked)
        {
            PathInfos pathInfos;
            if (filePath.LastIndexOf('\\') != filePath.Length - 1)
            {
                filePath = filePath + "\\";
            }
            DirectoryInfo directory = new DirectoryInfo(filePath);
            if (directory.Exists)
            {
                FileInfo[] fileInfos;
                DirectoryInfo[] directoryInfos;

                fileInfos = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
                directoryInfos = directory.GetDirectories("*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo fileInfo in fileInfos)
                {
                    pathInfos = new PathInfos();
                    pathInfos.FileName = fileInfo.Name;
                    pathInfos.FilePath = fileInfo.FullName;
                    pathInfos.FileSize = Math.Round(Double.Parse(fileInfo.Length.ToString()) / 1024,2);
                    pathInfos.FileType = fileInfo.Extension.Substring(1, fileInfo.Extension.Length - 1);
                    pathInfos.FileDate = fileInfo.CreationTime;
                    fileList.Add(pathInfos);
                }
                foreach (DirectoryInfo directoryInfo in directoryInfos)
                {
                    pathInfos = new PathInfos();
                    pathInfos.FileName = directoryInfo.Name;
                    pathInfos.FilePath = directoryInfo.FullName;
                    pathInfos.FileSize = Math.Round(Double.Parse(GetDirectorySize(directoryInfo.FullName).ToString()) / 1024, 2);
                    pathInfos.FileType = "文件夹";
                    pathInfos.FileDate = directoryInfo.CreationTime;
                    fileList.Add(pathInfos);
                }
                if (IsChecked)
                {

                    foreach (DirectoryInfo dirInfo in directoryInfos)
                    {
                        try
                        {
                            LoadFileByPath(ref fileList, dirInfo.FullName, IsChecked);
                        }
                        catch
                        {

                        }
                    }
                }
            }
            else
            {
                return 0;
            }
            return fileList.Count;
        }


        public static double GetDirectorySize(String filePath)
        {
            double size = 0;
            try
            {
                DirectoryInfo directory = new DirectoryInfo(filePath);
                FileInfo[] fileInfos = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo fileInfo in fileInfos)
                {
                    size += fileInfo.Length;
                }
            }
            catch { }

            return size;
        }

        //CsvHelper CSV导出
        public static string CSVExportByHelper(List<PathInfos> fileList, String exPath)
        {
            if (exPath.LastIndexOf("\\") != (exPath.Length - 1))
            {
                exPath = exPath + "\\";
            }
            String filePath = exPath + System.DateTime.Now.ToString().Replace(":", "-").Replace("/", "-") + " CSVResult.txt";

            using (var writer = new StreamWriter(filePath))
            using (var csv = new CsvWriter(writer))
            {
                csv.WriteHeader<PathInfos>();
                csv.NextRecord();
                foreach (PathInfos fileInfo in fileList)
                {
                    int index = fileInfo.FilePath.LastIndexOf("\\");
                    csv.WriteField(fileInfo.FileName);
                    csv.WriteField(fileInfo.FilePath.Substring(0, index + 1));
                    csv.WriteField(fileInfo.FileType);
                    csv.WriteField(Math.Round(fileInfo.FileSize / 1024, 2) + "KB");
                    csv.WriteField(fileInfo.FileDate);
                    csv.NextRecord();
                }
            }
            return filePath;
        }

        //CsvHelper CSV导入
        public static int CSVImportByHelper(ref List<PathInfos> fileList, String imPath)
        {
            using (var reader = new StreamReader(imPath))
            using (var csv = new CsvReader(reader))
            {
                try
                {
                    fileList.Clear();
                    csv.Read();
                    csv.ReadHeader();
                    while (csv.Read())
                    {
                        string fileName = csv.GetField<String>(0);
                        string filePath = csv.GetField<String>(1);
                        string fileType = csv.GetField<String>(2);
                        Double fileSize = double.Parse(csv.GetField<String>(3).Substring(0, csv.GetField<String>(3).Length - 2));
                        DateTime fileDate = csv.GetField<DateTime>(4);
                        PathInfos pathInfos = new PathInfos(fileName, filePath, fileType, fileSize, fileDate);
                        fileList.Add(pathInfos);
                    }
                }
                catch
                {
                    return 0;
                }
            }

            return fileList.Count;
        }
    }
}
